I wanted to have an independent system regardless where the root of this script uploaded. They could be uploaded on root server http://yousite.com, on subdomain http://yoursite.com/subdomain, in subdirectory http://yoursite.com/subdomain/subdirectory, or even in virtual subdomain http://subdomain.yoursite.com/. Finding an absolute server path was not as special as this one, because there are several adequate methods like using native getcwd() or $_SERVER.
The problem for URL is we cant tell where the site root without first knowing where your web root documents located at the server. A common structure like /usr/<user_name>/www/public_html as web root with http://yoursite.com URL would not be a problem since you could always define
"/" as your variable when pointing to root site. But when you move the your root site directory 1 level down, "/" would certainly break your script down. Or how if your website located on subdirectory of a virtual subdomain?Assumed you have this kind of structure:
..index.php
..init.php
../admin/
..index.php
init.php is the place where you save your CONSTANTs throughout the site and also constantly executed for root index and admin index. Here is how to find and absolute URL from inside init.php.<?php
# replace windows trailing slash \ with unix style /
$url = "http://" . $_SERVER['SERVER_NAME'] .
str_replace("\\", "/", dirname($_SERVER['SCRIPT_NAME']));
# need forward slash just in case we are in sub domain
$url = (substr($url, -1, 1) != "/") ? $url . "/" : $url;
# directory of this init.php
define('ABS_PATH', str_replace("\\", "/", dirname(__FILE__)));
$dir = str_replace("\\", "/", getcwd());
$tmp = str_replace(ABS_PATH . "/", '', $_SERVER['SCRIPT_FILENAME']);
if($dir != ABS_PATH)
{
# we're on subdirectory, strip the directory from $url
$tmp = explode('/', strrev($tmp), 2);
$url = str_replace("/" . strrev($tmp[1]), '', $url);
}
define('ABS_URL', $url);
?>On your root site index.php insert this init.php usingrequire_once('./init.php'); On your admin index.php use require_once('../init.php'); Note the difference between ./ (which mean on the same dir) and ../ (up one dir)ABS_PATH will always point to the directory where your
init.php located. This is real path (aka server path) of your root web documents. ABS_URL will always point to absolute URL location of directory of your init.php.Hope this help someone..
There are loads of facilities for couples at this lodge, including a bar, a restaurant, and a casino. Guests staying at Live Casino & Hotel - Philadelphia take pleasure in access to a on line casino, a 24-hour gym, and free WiFi in public areas. The 24-hour entrance desk has multilingual employees prepared to help with baggage storage, dry cleaning/laundry, 점보카지노 and securing valuables.
ReplyDelete